/**
 * Copyright (c) 2005-2010 springside.org.cn Licensed under the Apache License,
 * Version 2.0 (the "License"); $Id: ConvertUtils.java 1211 2010-09-10 16:20:45Z
 * calvinxiu $
 */
package com.dmagic.cms.common.util.reflection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dmagic.cms.common.util.DateUtil;



/**
 * [ticket id] Description of the class
 * 
 * @author songlin.li @ Version : V<Ver.No> <Jun 16, 2012>
 */
public class ConvertUtil extends org.apache.commons.beanutils.ConvertUtils {
    private static Logger logger = LoggerFactory.getLogger(ConvertUtil.class);
    static {
        registerDateConverter();
    }

    /**
     * Extraction of the objects in the collection attributes ( getter functions
     * ) , into the List.
     * 
     * @param collection Source collection.
     * @param propertyName Want to extract the attribute name.
     */
    @SuppressWarnings("rawtypes")
    public static List convertElementPropertyToList(final Collection<?> collection, final String propertyName) {
        List<Object> list = new ArrayList<Object>();
        try {
            for (Object obj : collection) {
                list.add(PropertyUtils.getProperty(obj, propertyName));
            }
        } catch (Exception e) {
            throw ReflectionUtil.convertReflectionExceptionToUnchecked(e);
        }
        return list;
    }

    /**
     * Extract the objects in the collection attributes ( through a getter
     * function ) , combined into a string separated by the separator.
     * 
     * @param collection Source collection.
     * @param propertyName Want to extract the attribute name.
     * @param separator separating character.
     */
    @SuppressWarnings("rawtypes")
    public static String convertElementPropertyToString(final Collection<?> collection, final String propertyName,
            final String separator) {
        List list = convertElementPropertyToList(collection, propertyName);
        return StringUtils.join(list, separator);
    }

    /**
     * Convert the string to the appropriate type.
     * 
     * @param value To be converted string.
     * @param toType Convert the target type.
     */
    public static Object convertStringToObject(String value, Class<?> toType) {
        try {
            return org.apache.commons.beanutils.ConvertUtils.convert(value, toType);
        } catch (Exception e) {
            throw ReflectionUtil.convertReflectionExceptionToUnchecked(e);
        }
    }

    /**
     * defined Converter format yyyy-MM-dd , yyyy-MM-dd HH:mm:ss or dd/MM/yyyy
     * dd/MM/yyyy HH:mm:ss
     */
    private static void registerDateConverter() {
        DateConverter dc = new DateConverter();
        dc.setUseLocaleFormat(true);
        dc.setPatterns(new String[] { DateUtil.FORMAT_1, DateUtil.FORMAT_2, DateUtil.FORMAT_3, DateUtil.FORMAT_4,
                DateUtil.FORMAT_5, DateUtil.FORMAT_6, DateUtil.FORMAT_7 });
        org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
    }
}
